/**
 * def header
 * 
 * @author strlst <e11907086@student.tuwien.ac.at>
 * @date 2020-11-06
 * @brief contains macro definitions and data structures used for consistent data exchange
 * @details it was considered to export def.h as a shared library, because of its purpose as a shared set of definitions between two symbiotic programs, but doing so would have increased the complexity of the assignment in probably undesired ways
 */

#ifndef DEF_H
#define DEF_H

#define OS_SUCCESS           0
#define OS_SHM_OPEN_FAILED   1
#define OS_SHM_CLOSE_FAILED  2
#define OS_MAP_FAILED        3
#define OS_UNMAP_FAILED      4
#define OS_SHM_UNLINK_FAILED 5
#define OS_FTRUNCATE_FAILED  6
#define OS_SEM_OPEN_FAILED   7
#define OS_SEM_CLOSE_FAILED  8
#define OS_SEM_UNLINK_FAILED 9
#define INVALID_INPUT        10
#define INVALID_REMOVE_EDGE  11
#define SHM_NAME             "/11907086_circular_buffer"
#define MAX_DATA             (512)
#define MAX_SOLUTIONS        (16)
#define SEM_ACCESS           "/11907086_sem_access"
#define SEM_USED             "/11907086_sem_used"
#define SEM_FREE             "/11907086_sem_free"
#define MAX_NODES            (MAX_DATA / 2)
#define INVALID_EDGE_INDEX   (-1)
#define INVALID_EDGE         (-1)
#define DELIMITER_EDGE       ((unsigned int) -1)

/**
 * @brief specifies available colors assignable to nodes within a graph
 */
enum color {
    RED   = 0x00, /**< color red */
    GREEN = 0x01, /**< color green */
    BLUE  = 0x02  /**< color blue */
};

/* TODO: implement automatic buffer resizing */
/**
 * @brief buffer to fetch results generated by other processes from
 * @details doesn't support dynamic resizing of shared memory, because that would be really complicated
 */
struct circular_buffer {
    int quit;                                    /**< shared variable used to terminate generator processes */
    size_t in;                                   /**< index pointing at processable solution entry */
    size_t out;                                  /**< index pointing at enterable solution entry */
    unsigned int edges[MAX_SOLUTIONS][MAX_DATA]; /**< solutions represented by arrays of edges */
};

/* TODO: implement automatic graph resizing */
/**
 * @brief structure representing a colored graph
 * @details assumes node_colors and node_ids are appended to only up to and in sync with node_count, also assumes edges and possibly invalidated are appended to only up to and in sync with edge_count
 */
struct graph {
    enum color node_colors[MAX_NODES]; /**< nodes are identified by their id and assigned a color */
    unsigned int node_ids [MAX_NODES]; /**< stores node indices used to retrieve their colors */
    size_t node_count;                 /**< stores length of node_colors and node_ids */
    unsigned int edges[2 * MAX_NODES]; /**< edges are stored as pair of individual node indices */
    int invalidated[2 * MAX_NODES];    /**< invalidated edges are stored as 0 or 1 integers at respective edge indices */
    size_t edge_count;                 /**< stores amount of edge indices */
};

#endif
